package com.wisdom.weld.convert;

import com.google.common.base.CaseFormat;
import com.jayway.jsonpath.JsonPath;
import com.wisdom.weld.enumeration.JavaType;
import com.wisdom.weld.enumeration.JdbcType;
import com.wisdom.weld.pojo.ClassAttribute;
import com.wisdom.weld.pojo.ColumnAttribute;
import com.wisdom.weld.pojo.FieldAttribute;
import com.wisdom.weld.pojo.TableAttribute;
import org.apache.commons.collections4.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JavaObjectConverter {

    private static final Map<String, String> MAPPING = new HashMap<>();

    static {
        MAPPING.put("VARCHAR", "String");
        MAPPING.put("CHAR", "String");
        MAPPING.put("TEXT", "String");
        MAPPING.put("TINYINT", "Integer");
        MAPPING.put("SMALLINT", "Integer");
        MAPPING.put("SMALLINT", "Integer");
        MAPPING.put("INT", "Integer");
        MAPPING.put("BIGINT", "Long");
        MAPPING.put("BIGINT UNSIGNED", "Long");
        MAPPING.put("FLOAT", "Float");
        MAPPING.put("DOUBLE", "Double");
        MAPPING.put("DATE", "Date");
        MAPPING.put("TIME", "Date");
        MAPPING.put("TIMESTAMP", "Date");
        MAPPING.put("DATETIME", "Date");
    }


    public static List<ClassAttribute> convert(List<TableAttribute> tableAttributes) {
        if (CollectionUtils.isEmpty(tableAttributes)) {
            throw new RuntimeException("转换对象为空");
        }
        List<ClassAttribute> classAttributes = new ArrayList<>();
        try {
            for (TableAttribute tableAttribute : tableAttributes) {
                ClassAttribute classAttribute = new ClassAttribute();
                classAttribute.setClassName(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableAttribute.getTableName()));
                URL resource = ClassLoader.getSystemResource("generator.json");
                classAttribute.setPackageName(JsonPath.read(new File(resource.getPath()), "$.generator.base-package"));
                classAttribute.setRemark(tableAttribute.getComment());
                classAttribute.setTableName(tableAttribute.getTableName());
                classAttributes.add(classAttribute);
                List<ColumnAttribute> columnAttributes = tableAttribute.getColumnAttributes();
                if (CollectionUtils.isEmpty(columnAttributes)) {
                    continue;
                }
                List<FieldAttribute> fieldAttributes = new ArrayList<>();
                for (ColumnAttribute columnAttribute : columnAttributes) {
                    FieldAttribute fieldAttribute = new FieldAttribute();
                    fieldAttribute.setFieldName(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnAttribute.getColumnName()));
                    fieldAttribute.setRemark(columnAttribute.getComment());
                    fieldAttribute.setType(MAPPING.get(columnAttribute.getTypeName()));
                    fieldAttribute.setColumnName(columnAttribute.getColumnName());
                    fieldAttribute.setIsPrimaryKey(columnAttribute.getIsPrimaryKey());
                    fieldAttributes.add(fieldAttribute);
                }
                classAttribute.setFieldAttributes(fieldAttributes);
            }
        } catch (IOException e) {
            throw new RuntimeException("JSON PATH 读取数据异常",e);
        }
        return classAttributes;
    }

}